﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using HWR_Ants.Classes;
using HWR_Ants.Tools;


namespace HWR_Ants.Forms
{
    public partial class fOptions : Form
    {
        private static string optionsFile = Tools.Tools.OPTIONSFILE;
        private static string defaultFile = Tools.Tools.DEFAULTOPTIONSFILE;
        private System.Windows.Forms.ToolTip labelsToolTip;

        public fOptions()
        {
            InitializeComponent();
            fillTextboxes(loadOptions());
            this.labelsToolTip = new System.Windows.Forms.ToolTip(new System.ComponentModel.Container());
            labelsToolTip.Active = false;
            this.SuspendLayout();
        }

        #region Button Clicks

        private void bt_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_Save_Click(object sender, EventArgs e)
        {
            try
            {
                Options o = new Options();
                o.AntsCounter = Convert.ToInt32(tb_AntsCount.Text);
                o.CityCounter = Convert.ToInt32(tb_CityCount.Text);
                o.IterationCounter = Convert.ToInt32(tb_IterationCount.Text);
                o.Schwellenwert = Convert.ToDouble(tb_schwellenwert.Text);
                o.Parameter.Alpha = Convert.ToDouble(tb_Param_Phero.Text);
                o.Parameter.Beta = Convert.ToDouble(tb_Param_Locale.Text);
                o.Parameter.Rho = Convert.ToDouble(tb_Param_Verdunstung.Text);
                o.Parameter.Tau = Convert.ToDouble(tb_Param_Initial_Pheromon.Text);
                o.Parameter.Q = Convert.ToDouble(tb_Param_Pheromon_Update.Text);
                o.Stopp = cb_stoppkreterium.Text.ToString();

                saveOptions(optionsFile, o);

                this.Close();
                Tools.Tools.ActiveCurrentOptions = Tools.Tools.getCurrentStoredOptions();
            }
            catch
            {
                fillTextboxes(loadOptions(defaultFile));
                MessageBox.Show("Es ist ein Fehler aufgetreten. Defaults geladen.", "Error");
            }
        }

        private void bt_LoadDefaults_Click(object sender, EventArgs e)
        {
            Options o = loadOptions(defaultFile);
            fillTextboxes(o);
            // saveOptions(optionsFile, o);
        }

        #endregion

        #region needed Functions

        private void saveOptions(string filename, Options o)
        {
            Tools.Tools.writeXML<Options>(o, filename);
        }

        private void fillTextboxes(Options o)
        {
            tb_AntsCount.Text = o.AntsCounter.ToString();
            tb_CityCount.Text = o.CityCounter.ToString();
            tb_IterationCount.Text = o.IterationCounter.ToString();
            tb_schwellenwert.Text = o.Schwellenwert.ToString();
            cb_stoppkreterium.Text = o.Stopp.ToString();

            tb_Param_Phero.Text = o.Parameter.Alpha.ToString();
            tb_Param_Locale.Text = o.Parameter.Beta.ToString();
            tb_Param_Verdunstung.Text = o.Parameter.Rho.ToString();
            tb_Param_Initial_Pheromon.Text = o.Parameter.Tau.ToString();
            tb_Param_Pheromon_Update.Text = o.Parameter.Q.ToString();
        }

        public Options loadOptions()
        {
            return loadOptions(optionsFile);
        }

        private Options loadOptions(string Filename)
        {
            try
            {
                Options o;
                if (File.Exists(Filename))
                {
                    o = Tools.Tools.readXML<Options>(Filename);
                }
                else
                {
                    o = Tools.Tools.getCurrentStoredOptions();
                    // Tools.Tools.writeXML<Options>(o, Filename);
                }
                return o;
            }
            catch
            {
                MessageBox.Show("Es ist ein Fehler beim laden der Optionen aufgetreten.", "Error");
            }
            return new Options();
        }

        #endregion

        #region Tooltips

        private void lb_help_antcount_Click(object sender, EventArgs e)
        {
            if (!labelsToolTip.Active)
            {
                labelsToolTip.Show("legt die Anzahl der Ameisen fest,\ndie den Weg ablaufen sollen \nmin: 1", lb_help_antcount);
                labelsToolTip.Active = true;
            }
            else
            {
                labelsToolTip.Hide(lb_help_antcount);
                labelsToolTip.Active = false;
            }
        }

        private void lb_help_antcount_MouseLeave(object sender, EventArgs e)
        {
            labelsToolTip.Hide(lb_help_antcount);
            labelsToolTip.Active = false;
        }
        
        private void lb_help_citycount_Click(object sender, EventArgs e)
        {
            if (!labelsToolTip.Active)
            {
                labelsToolTip.Show("legt die Anzahl der Städte fest, \n die abgelaufen werden sollen \nmin: 3", lb_help_citycount);
                labelsToolTip.Active = true;
            }
            else
            {
                labelsToolTip.Hide(lb_help_citycount);
                labelsToolTip.Active = false;
            }
           
        }
        
        private void lb_help_citycount_MouseLeave(object sender, EventArgs e)
        {
            labelsToolTip.Hide(lb_help_citycount);
            labelsToolTip.Active = false;
        }

        private void lb_help_iterationcount_Click(object sender, EventArgs e)
        {
            if (!labelsToolTip.Active)
            {
                labelsToolTip.Show("legt die Anzahl der zu laufenen Touren fest \nmin: 1; Empfohlen: min: 100", lb_help_iterationcount);
                labelsToolTip.Active = true;
            }
            else
            {
                labelsToolTip.Hide(lb_help_iterationcount);
                labelsToolTip.Active = false;
            }

        }
     
        private void lb_help_iterationcount_MouseLeave(object sender, EventArgs e)
        {
            labelsToolTip.Hide(lb_help_iterationcount);
            labelsToolTip.Active = false;
        }

        private void lb_help_stoppoption_Click(object sender, EventArgs e)
        {
            if (!labelsToolTip.Active)
            {
                labelsToolTip.Show("legt das Stopp Kriterium fest, nachdem die Suche \n der besten Tour gesucht werden soll \n (Iteration/Schwellenwert/Lösung)", lb_help_stoppoption);
                labelsToolTip.Active = true;
            }
            else
            {
                labelsToolTip.Hide(lb_help_stoppoption);
                labelsToolTip.Active = false;
            }
        }

        private void lb_help_stoppoption_MouseLeave(object sender, EventArgs e)
        {
            labelsToolTip.Hide(lb_help_stoppoption);
            labelsToolTip.Active = false;
        }

        private void lb_help_pheromonparameter_Click(object sender, EventArgs e)
        {
            if (!labelsToolTip.Active)
            {
                labelsToolTip.Show("heuristischer Wert für die Pheromone\n(t0 > 0)", lb_help_pheromonparameter);
                labelsToolTip.Active = true;
            }
            else
            {
                labelsToolTip.Hide(lb_help_pheromonparameter);
                labelsToolTip.Active = false;
            }
        }

        private void lb_help_pheromonparameter_MouseLeave(object sender, EventArgs e)
        {
            labelsToolTip.Hide(lb_help_pheromonparameter);
            labelsToolTip.Active = false;
        }

        private void lb_help_localparameter_Click(object sender, EventArgs e)
        {
            if (!labelsToolTip.Active)
            {
                labelsToolTip.Show("heuristischer Wert für die \nlokale Information \n(β > 0) ", lb_help_localparameter);
                labelsToolTip.Active = true;
            }
            else
            {
                labelsToolTip.Hide(lb_help_localparameter);
                labelsToolTip.Active = false;
            }
        }

        private void lb_help_localparameter_MouseLeave(object sender, EventArgs e)
        {
            labelsToolTip.Hide(lb_help_localparameter);
            labelsToolTip.Active = false;
        }

        private void lb_help_verdunstungsfaktor_Click(object sender, EventArgs e)
        {
            if (!labelsToolTip.Active)
            {
                labelsToolTip.Show("Der Verdunstungsfaktor (ρ) legt fest wie schnell \ndie Pheromone verschwinden, wenn \nder Weg nicht genutzt wird \n(0 < ρ <= 1)", lb_help_verdunstungsfaktor);
                labelsToolTip.Active = true;
            }
            else
            {
                labelsToolTip.Hide(lb_help_verdunstungsfaktor);
                labelsToolTip.Active = false;
            }
        }

        private void lb_help_verdunstungsfaktor_MouseLeave(object sender, EventArgs e)
        {
            labelsToolTip.Hide(lb_help_verdunstungsfaktor);
            labelsToolTip.Active = false;
        }

        private void lb_help_initial_pheromonwerte_Click(object sender, EventArgs e)
        {
            if (!labelsToolTip.Active)
            {
                labelsToolTip.Show("ist der Pheromonwert aller Wege beim Start \n(t0 > 0)", lb_help_initial_pheromonwerte);
                labelsToolTip.Active = true;
            }
            else
            {
                labelsToolTip.Hide(lb_help_initial_pheromonwerte);
                labelsToolTip.Active = false;
            }
        }

        private void lb_help_initial_pheromonwerte_MouseLeave(object sender, EventArgs e)
        {
            labelsToolTip.Hide(lb_help_initial_pheromonwerte);
            labelsToolTip.Active = false;
        }

        private void lb_help_pheromonupdate_Click(object sender, EventArgs e)
        {
            if (!labelsToolTip.Active)
            {
                labelsToolTip.Show("Aktualisierungswert, der nach einer Tour \n einem jeweiligen abeglaufenden Weg nugeorndnet wird \n0< u <= 1", lb_help_pheromonupdate);
                labelsToolTip.Active = true;
            }
            else
            {
                labelsToolTip.Hide(lb_help_pheromonupdate);
                labelsToolTip.Active = false;
            }
        }

        private void lb_help_pheromonupdate_MouseLeave(object sender, EventArgs e)
        {
            labelsToolTip.Hide(lb_help_pheromonupdate);
            labelsToolTip.Active = false;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            if (!labelsToolTip.Active)
            {
                labelsToolTip.Show("Tourlänge bei derer Unterschreitung gestoppt wird", lb_help_schwellenwert);
                labelsToolTip.Active = true;
            }
            else
            {
                labelsToolTip.Hide(lb_help_schwellenwert);
                labelsToolTip.Active = false;
            }
        }

        #endregion

        private void cb_stoppkreterium_SelectedIndexChanged(object sender, EventArgs e)
        {
            string temp = cb_stoppkreterium.Text.ToString();
            switch (temp)
            {
                case "Iteration":
                    tb_schwellenwert.Enabled = false;
                    tb_IterationCount.Enabled = true;
                    break;
                case "Schwellenwert":
                    tb_IterationCount.Enabled = false;
                    tb_schwellenwert.Enabled = true;
                    break;
                case "Lösung":
                    tb_IterationCount.Enabled = true;
                    tb_schwellenwert.Enabled = true;
                    break;
            }

        }
        
    }
}
